/*******************************************************************************
 * Copyright (c) Jerges A. Bello G. 2014. Reservado todos los derechos.
 ******************************************************************************/
package com.jabex.common.to;

import com.jabex.common.exceptions.ErrorFunctionalException;
import com.jabex.common.exceptions.ErrorOperationalException;
import com.jabex.common.exceptions.WarningFunctionalException;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jaugusto on 26/08/14. *
 */
public class ResponseMessageTO<E> implements Serializable {
	private static final long serialVersionUID = 1L;

	private List<ErrorFunctionalException> functionalErrors;
	private List<ErrorOperationalException> operationalErrors;
	private List<WarningFunctionalException> warns;
	private List<MessageWithParamsTO> infoMessages;
	private List<MessageWithParamsTO> errorMessages;
	private List<MessageWithParamsTO> operationalErrorsMessages;
	private List<MessageWithParamsTO> warnMessages;
	private E object;

	public ResponseMessageTO() {
		super();
	}

	public ResponseMessageTO(ResponseMessageTO<E> ir) {
		super();
		this.object = ir.object;
		this.functionalErrors = ir.functionalErrors;
		this.operationalErrors = ir.operationalErrors;
		this.warns = ir.warns;
		this.infoMessages = ir.infoMessages;
		this.errorMessages = ir.errorMessages;
		this.warnMessages = ir.warnMessages;
	}

	public void addInfoMessage(String message, Object... params) {
		MessageWithParamsTO m = new MessageWithParamsTO(message, params);
		if (infoMessages == null) {
			infoMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		infoMessages.add(m);
	}

	public void addErrorMessage(String message, Object... params) {
		MessageWithParamsTO m = new MessageWithParamsTO(message, params);
		if (errorMessages == null) {
			errorMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		errorMessages.add(m);
	}

	public void addWarnMessage(String message, Object... params) {
		MessageWithParamsTO m = new MessageWithParamsTO(message, params);
		if (warnMessages == null) {
			warnMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		warnMessages.add(m);
	}


	public List<ErrorOperationalException> getOperationalErrors() {
		if (operationalErrors == null)
			operationalErrors = new ArrayList<ErrorOperationalException>(2);
		return operationalErrors;
	}


	public List<ErrorFunctionalException> getFunctionalErrors() {
		if (functionalErrors == null)
			functionalErrors = new ArrayList<ErrorFunctionalException>(2);
		return functionalErrors;
	}

	public List<WarningFunctionalException> getWarns() {
		if (warns == null)
			warns = new ArrayList<WarningFunctionalException>(2);
		return warns;
	}

	public List<MessageWithParamsTO> getWarnMessages() {
		if (warnMessages == null) {
			warnMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		return warnMessages;
	}

	public List<MessageWithParamsTO> getInfoMessages() {
		if (infoMessages == null) {
			infoMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		return infoMessages;
	}

	/**
	 * @return the errorMessages (Functional Errors)
	 */
	public List<MessageWithParamsTO> getErrorMessages() {
		if (errorMessages == null) {
			errorMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		return errorMessages;
	}

	/**
	 * @return the operationalErrorsMessages (Operationals Errors)
	 */
	public List<MessageWithParamsTO> getOperationalErrorsMessages() {
		if (operationalErrorsMessages == null) {
			operationalErrorsMessages = new ArrayList<MessageWithParamsTO>(2);
		}
		return operationalErrorsMessages;
	}

	/**
	 * @return the object
	 */
	public E getObject() {
		return object;
	}

	/**
	 * @param object the object to set
	 */
	public void setObject(E object) {
		this.object = object;
	}

	public boolean hasFunctionalErrorMessages() {
		return errorMessages != null && !errorMessages.isEmpty();
	}

	public boolean hasErrorOperationalMessages() {
		return operationalErrorsMessages != null && !operationalErrorsMessages.isEmpty();
	}

	public boolean hasWarnMessages() {
		return warnMessages != null && !warnMessages.isEmpty();
	}

	public boolean hasInfoMessages() {
		return infoMessages != null && !infoMessages.isEmpty();
	}

	public boolean hasFunctionalErrors() {
		return functionalErrors != null && !functionalErrors.isEmpty();
	}

	public boolean hasWarns() {
		return warns != null && !warns.isEmpty();
	}


	public void addMessages(ResponseMessageTO<E> results) {
		if (results.hasFunctionalErrorMessages()) {
			getErrorMessages().addAll(results.getErrorMessages());
		}
		if (results.hasErrorOperationalMessages()) {
			getOperationalErrorsMessages().addAll(results.getOperationalErrorsMessages());
		}
		if (results.hasWarnMessages()) {
			getWarnMessages().addAll(results.getWarnMessages());
		}
		if (results.hasInfoMessages()) {
			getInfoMessages().addAll(results.getInfoMessages());
		}
		if (results.hasFunctionalErrors()) {
			getFunctionalErrors().addAll(results.getFunctionalErrors());
		}
		if (results.hasWarns()) {
			getWarns().addAll(results.getWarns());
		}

	}
}
